<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
  <meta charset="utf-8" />
  <meta name="generator" content="pandoc" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
  <title>基于K8s的灰度发布配置</title>
  <style type="text/css">
      code{white-space: pre-wrap;}
      span.smallcaps{font-variant: small-caps;}
      span.underline{text-decoration: underline;}
      div.column{display: inline-block; vertical-align: top; width: 50%;}
  </style>
  <style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
  { position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
  { content: attr(title);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; pointer-events: all; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
  </style>
</head>
<body>
<h2 id="基于k8s的灰度发布配置">基于K8s的灰度发布配置</h2>
<h5 id="这里只对基于ingress和configmap两种方式进行了演示一种是流量控制一种是configmap配置控制">这里只对基于ingress和configmap两种方式进行了演示，一种是流量控制，一种是configmap配置控制。</h5>
<h5 id="ingress适用于整体版本更新迭代或者是某个页面的局部更新">ingress：适用于整体版本更新迭代或者是某个页面的局部更新。</h5>
<h5 id="configmap可以结合代码对指定地理区域或者指定机型ios安卓pc进行推送新版本">configmap：可以结合代码对指定地理区域或者指定机型（ios、安卓、pc）进行推送新版本。</h5>
<h3 id="一安装ingress-nginx">一、安装ingress-nginx</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb1-1" title="1"><span class="co">#安装教程可以看另一篇文章</span></a>
<a class="sourceLine" id="cb1-2" title="2"></a>
<a class="sourceLine" id="cb1-3" title="3">安装的是<span class="ex">2025</span>年7月份最新的ingress版本</a>
<a class="sourceLine" id="cb1-4" title="4"></a>
<a class="sourceLine" id="cb1-5" title="5"><span class="ex">https</span>://helloops.cn/k8s/install_k8s_1_30_1.html</a></code></pre></div>
<h3 id="二配置ingress-权重路由精准流量切分">二、配置Ingress 权重路由（精准流量切分）</h3>
<h5 id="原理通过-ingress-控制器如-nginx-ingress的权重配置将指定比例的流量路由到新版本-service无需依赖副本数">原理：通过 Ingress 控制器（如 Nginx Ingress）的权重配置，将指定比例的流量路由到新版本 Service，无需依赖副本数。</h5>
<h5 id="使用场景精确流量控制v15-v295">使用场景：精确流量控制（V1=5% , V2=95%）</h5>
<h4 id="创建两个不同版本的nginx-deployment并配置svc">1、创建两个不同版本的nginx deployment,并配置svc</h4>
<div class="sourceCode" id="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb2-1" title="1"><span class="ex">vim</span> nginx-deployment.yaml</a>
<a class="sourceLine" id="cb2-2" title="2"></a>
<a class="sourceLine" id="cb2-3" title="3"><span class="ex">apiVersion</span>: apps/v1</a>
<a class="sourceLine" id="cb2-4" title="4"><span class="ex">kind</span>: Deployment</a>
<a class="sourceLine" id="cb2-5" title="5"><span class="ex">metadata</span>:</a>
<a class="sourceLine" id="cb2-6" title="6">  <span class="ex">name</span>: nginx-v1</a>
<a class="sourceLine" id="cb2-7" title="7"><span class="ex">spec</span>:</a>
<a class="sourceLine" id="cb2-8" title="8">  <span class="ex">replicas</span>: 2  <span class="co">#初始副本数</span></a>
<a class="sourceLine" id="cb2-9" title="9">  <span class="ex">selector</span>:</a>
<a class="sourceLine" id="cb2-10" title="10">    <span class="ex">matchLabels</span>:</a>
<a class="sourceLine" id="cb2-11" title="11">      <span class="ex">app</span>: nginx</a>
<a class="sourceLine" id="cb2-12" title="12">      <span class="ex">version</span>: v1</a>
<a class="sourceLine" id="cb2-13" title="13">  <span class="ex">strategy</span>:</a>
<a class="sourceLine" id="cb2-14" title="14">    <span class="ex">rollingUpdate</span>:</a>
<a class="sourceLine" id="cb2-15" title="15">      <span class="ex">maxSurge</span>: 1   <span class="co"># 最多比期望副本数多1个 （用于控制新版本创建速度）</span></a>
<a class="sourceLine" id="cb2-16" title="16">      <span class="ex">maxUnavailable</span>: 0  <span class="co">#更新过程中不可用的Pod数为0 （用于保证服务的连续性）</span></a>
<a class="sourceLine" id="cb2-17" title="17">    <span class="ex">type</span>: RollingUpdate</a>
<a class="sourceLine" id="cb2-18" title="18">  <span class="ex">template</span>:</a>
<a class="sourceLine" id="cb2-19" title="19">    <span class="ex">metadata</span>:</a>
<a class="sourceLine" id="cb2-20" title="20">      <span class="ex">labels</span>:</a>
<a class="sourceLine" id="cb2-21" title="21">        <span class="ex">app</span>: nginx</a>
<a class="sourceLine" id="cb2-22" title="22">        <span class="ex">version</span>: v1</a>
<a class="sourceLine" id="cb2-23" title="23">    <span class="ex">spec</span>:</a>
<a class="sourceLine" id="cb2-24" title="24">      <span class="ex">containers</span>:</a>
<a class="sourceLine" id="cb2-25" title="25">      <span class="ex">-</span> name: nginx</a>
<a class="sourceLine" id="cb2-26" title="26">        <span class="ex">image</span>: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:1.27.4  </a>
<a class="sourceLine" id="cb2-27" title="27">        <span class="ex">ports</span>:</a>
<a class="sourceLine" id="cb2-28" title="28">        <span class="ex">-</span> containerPort: 80</a>
<a class="sourceLine" id="cb2-29" title="29"></a>
<a class="sourceLine" id="cb2-30" title="30"><span class="ex">---</span> </a>
<a class="sourceLine" id="cb2-31" title="31"><span class="ex">apiVersion</span>: apps/v1</a>
<a class="sourceLine" id="cb2-32" title="32"><span class="ex">kind</span>: Deployment</a>
<a class="sourceLine" id="cb2-33" title="33"><span class="ex">metadata</span>:</a>
<a class="sourceLine" id="cb2-34" title="34">  <span class="ex">name</span>: nginx-v2</a>
<a class="sourceLine" id="cb2-35" title="35"><span class="ex">spec</span>:</a>
<a class="sourceLine" id="cb2-36" title="36">  <span class="ex">replicas</span>: 2  <span class="co">#初始副本数</span></a>
<a class="sourceLine" id="cb2-37" title="37">  <span class="ex">selector</span>:</a>
<a class="sourceLine" id="cb2-38" title="38">    <span class="ex">matchLabels</span>:</a>
<a class="sourceLine" id="cb2-39" title="39">      <span class="ex">app</span>: nginx</a>
<a class="sourceLine" id="cb2-40" title="40">      <span class="ex">version</span>: v2</a>
<a class="sourceLine" id="cb2-41" title="41">  <span class="ex">strategy</span>:</a>
<a class="sourceLine" id="cb2-42" title="42">    <span class="ex">rollingUpdate</span>:</a>
<a class="sourceLine" id="cb2-43" title="43">      <span class="ex">maxSurge</span>: 1   <span class="co"># 最多比期望副本数多1个 （用于控制新版本创建速度）</span></a>
<a class="sourceLine" id="cb2-44" title="44">      <span class="ex">maxUnavailable</span>: 0  <span class="co">#更新过程中不可用的Pod数为0 （用于保证服务的连续性）</span></a>
<a class="sourceLine" id="cb2-45" title="45">    <span class="ex">type</span>: RollingUpdate</a>
<a class="sourceLine" id="cb2-46" title="46">  <span class="ex">template</span>:</a>
<a class="sourceLine" id="cb2-47" title="47">    <span class="ex">metadata</span>:</a>
<a class="sourceLine" id="cb2-48" title="48">      <span class="ex">labels</span>:</a>
<a class="sourceLine" id="cb2-49" title="49">        <span class="ex">app</span>: nginx</a>
<a class="sourceLine" id="cb2-50" title="50">        <span class="ex">version</span>: v2</a>
<a class="sourceLine" id="cb2-51" title="51">    <span class="ex">spec</span>:</a>
<a class="sourceLine" id="cb2-52" title="52">      <span class="ex">containers</span>:</a>
<a class="sourceLine" id="cb2-53" title="53">      <span class="ex">-</span> name: nginx</a>
<a class="sourceLine" id="cb2-54" title="54">        <span class="ex">image</span>: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:1.28.0  </a>
<a class="sourceLine" id="cb2-55" title="55">        <span class="ex">ports</span>:</a>
<a class="sourceLine" id="cb2-56" title="56">        <span class="ex">-</span> containerPort: 80</a>
<a class="sourceLine" id="cb2-57" title="57"></a>
<a class="sourceLine" id="cb2-58" title="58"><span class="ex">---</span></a>
<a class="sourceLine" id="cb2-59" title="59"><span class="ex">apiVersion</span>: v1</a>
<a class="sourceLine" id="cb2-60" title="60"><span class="ex">kind</span>: Service</a>
<a class="sourceLine" id="cb2-61" title="61"><span class="ex">metadata</span>:</a>
<a class="sourceLine" id="cb2-62" title="62">  <span class="ex">name</span>: nginx-svc-v1</a>
<a class="sourceLine" id="cb2-63" title="63"><span class="ex">spec</span>:</a>
<a class="sourceLine" id="cb2-64" title="64">  <span class="ex">selector</span>:</a>
<a class="sourceLine" id="cb2-65" title="65">    <span class="ex">app</span>: nginx</a>
<a class="sourceLine" id="cb2-66" title="66">    <span class="ex">version</span>: v1</a>
<a class="sourceLine" id="cb2-67" title="67">  <span class="ex">ports</span>:</a>
<a class="sourceLine" id="cb2-68" title="68">  <span class="ex">-</span> port: 8080</a>
<a class="sourceLine" id="cb2-69" title="69">    <span class="ex">targetPort</span>: 80</a>
<a class="sourceLine" id="cb2-70" title="70"></a>
<a class="sourceLine" id="cb2-71" title="71"><span class="ex">---</span></a>
<a class="sourceLine" id="cb2-72" title="72"><span class="ex">apiVersion</span>: v1</a>
<a class="sourceLine" id="cb2-73" title="73"><span class="ex">kind</span>: Service</a>
<a class="sourceLine" id="cb2-74" title="74"><span class="ex">metadata</span>:</a>
<a class="sourceLine" id="cb2-75" title="75">  <span class="ex">name</span>: nginx-svc-v2</a>
<a class="sourceLine" id="cb2-76" title="76"><span class="ex">spec</span>:</a>
<a class="sourceLine" id="cb2-77" title="77">  <span class="ex">selector</span>:</a>
<a class="sourceLine" id="cb2-78" title="78">    <span class="ex">app</span>: nginx</a>
<a class="sourceLine" id="cb2-79" title="79">    <span class="ex">version</span>: v2</a>
<a class="sourceLine" id="cb2-80" title="80">  <span class="ex">ports</span>:</a>
<a class="sourceLine" id="cb2-81" title="81">  <span class="ex">-</span> port: 8080</a>
<a class="sourceLine" id="cb2-82" title="82">    <span class="ex">targetPort</span>: 80</a></code></pre></div>
<h5 id="部署deployment和service">部署deployment和service</h5>
<pre><code>kubectl apply -f nginx-deployment.yaml</code></pre>
<h5 id="测试通过svc访问nginx">测试通过svc访问nginx</h5>
<pre><code>#通过yaml获取已创建的资源列表

kubectl get -f nginx-deployment.yaml

NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-v1   2/2     2            2           37m
deployment.apps/nginx-v2   2/2     2            2           37m

NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/nginx-svc-v1   ClusterIP   10.98.183.230   &lt;none&gt;        8080/TCP   37m
service/nginx-svc-v2   ClusterIP   10.97.71.111    &lt;none&gt;        8080/TCP   37m

# 能看到nginx版本号即可

curl 10.98.183.230:8080/nginx_status

curl 10.97.71.111:8080/nginx_status</code></pre>
<h4 id="创建ingress-nginx">2、创建ingress-nginx</h4>
<div class="sourceCode" id="cb5"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb5-1" title="1"><span class="ex">vim</span> ingress-ng-svc.yaml</a>
<a class="sourceLine" id="cb5-2" title="2"></a>
<a class="sourceLine" id="cb5-3" title="3"><span class="ex">apiVersion</span>: networking.k8s.io/v1</a>
<a class="sourceLine" id="cb5-4" title="4"><span class="ex">kind</span>: Ingress</a>
<a class="sourceLine" id="cb5-5" title="5"><span class="ex">metadata</span>:</a>
<a class="sourceLine" id="cb5-6" title="6">  <span class="ex">name</span>: nginx-ingress-primary</a>
<a class="sourceLine" id="cb5-7" title="7">  <span class="ex">annotations</span>:</a>
<a class="sourceLine" id="cb5-8" title="8">    <span class="ex">kubernetes.io</span>/ingress.class: <span class="st">&quot;nginx&quot;</span></a>
<a class="sourceLine" id="cb5-9" title="9"><span class="ex">spec</span>:</a>
<a class="sourceLine" id="cb5-10" title="10">  <span class="ex">rules</span>:</a>
<a class="sourceLine" id="cb5-11" title="11">  <span class="ex">-</span> host: nginx.ops.com</a>
<a class="sourceLine" id="cb5-12" title="12">    <span class="ex">http</span>:</a>
<a class="sourceLine" id="cb5-13" title="13">      <span class="ex">paths</span>:</a>
<a class="sourceLine" id="cb5-14" title="14">      <span class="ex">-</span> path: /</a>
<a class="sourceLine" id="cb5-15" title="15">        <span class="ex">pathType</span>: Prefix</a>
<a class="sourceLine" id="cb5-16" title="16">        <span class="ex">backend</span>:</a>
<a class="sourceLine" id="cb5-17" title="17">          <span class="ex">service</span>:</a>
<a class="sourceLine" id="cb5-18" title="18">            <span class="ex">name</span>: nginx-svc-v1</a>
<a class="sourceLine" id="cb5-19" title="19">            <span class="ex">port</span>:</a>
<a class="sourceLine" id="cb5-20" title="20">              <span class="ex">number</span>: 80</a>
<a class="sourceLine" id="cb5-21" title="21"></a>
<a class="sourceLine" id="cb5-22" title="22"><span class="ex">---</span></a>
<a class="sourceLine" id="cb5-23" title="23"><span class="ex">apiVersion</span>: networking.k8s.io/v1</a>
<a class="sourceLine" id="cb5-24" title="24"><span class="ex">kind</span>: Ingress</a>
<a class="sourceLine" id="cb5-25" title="25"><span class="ex">metadata</span>:</a>
<a class="sourceLine" id="cb5-26" title="26">  <span class="ex">name</span>: nginx-ingress-canary</a>
<a class="sourceLine" id="cb5-27" title="27">  <span class="ex">annotations</span>:</a>
<a class="sourceLine" id="cb5-28" title="28">    <span class="ex">kubernetes.io</span>/ingress.class: <span class="st">&quot;nginx&quot;</span></a>
<a class="sourceLine" id="cb5-29" title="29">    <span class="ex">nginx.ingress.kubernetes.io</span>/canary: <span class="st">&quot;true&quot;</span> <span class="co">#启用金丝雀</span></a>
<a class="sourceLine" id="cb5-30" title="30">    <span class="ex">nginx.ingress.kubernetes.io</span>/canary-weight: <span class="st">&quot;50&quot;</span></a>
<a class="sourceLine" id="cb5-31" title="31"><span class="ex">spec</span>:</a>
<a class="sourceLine" id="cb5-32" title="32">  <span class="ex">rules</span>:</a>
<a class="sourceLine" id="cb5-33" title="33">  <span class="ex">-</span> host: nginx.ops.com</a>
<a class="sourceLine" id="cb5-34" title="34">    <span class="ex">http</span>:</a>
<a class="sourceLine" id="cb5-35" title="35">      <span class="ex">paths</span>:</a>
<a class="sourceLine" id="cb5-36" title="36">      <span class="ex">-</span> path: /</a>
<a class="sourceLine" id="cb5-37" title="37">        <span class="ex">pathType</span>: Prefix</a>
<a class="sourceLine" id="cb5-38" title="38">        <span class="ex">backend</span>:</a>
<a class="sourceLine" id="cb5-39" title="39">          <span class="ex">service</span>:</a>
<a class="sourceLine" id="cb5-40" title="40">            <span class="ex">name</span>: nginx-svc-v2</a>
<a class="sourceLine" id="cb5-41" title="41">            <span class="ex">port</span>:</a>
<a class="sourceLine" id="cb5-42" title="42">              <span class="ex">number</span>: 80</a></code></pre></div>
<h4 id="部署ingress">3、部署ingress</h4>
<div class="sourceCode" id="cb6"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb6-1" title="1"><span class="ex">kubectl</span> apply -f ingress-ng-svc.yaml</a>
<a class="sourceLine" id="cb6-2" title="2"></a>
<a class="sourceLine" id="cb6-3" title="3"><span class="ex">kubectl</span> get -f ingress-ng-svc.yaml</a>
<a class="sourceLine" id="cb6-4" title="4"></a>
<a class="sourceLine" id="cb6-5" title="5"><span class="ex">NAME</span>                    CLASS    HOSTS           ADDRESS         PORTS   AGE</a>
<a class="sourceLine" id="cb6-6" title="6"><span class="ex">nginx-ingress-primary</span>   <span class="op">&lt;</span>none<span class="op">&gt;</span>   nginx.ops.com   10.110.74.177   80      29m</a>
<a class="sourceLine" id="cb6-7" title="7"><span class="ex">nginx-ingress-canary</span>    <span class="op">&lt;</span>none<span class="op">&gt;</span>   nginx.ops.com   10.110.74.177   80      29m</a></code></pre></div>
<h4 id="测试流量控制是否生效">4、测试流量控制是否生效</h4>
<div class="sourceCode" id="cb7"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb7-1" title="1"><span class="co">#如果和我一样是内网虚拟机，需要手动添加域名解析</span></a>
<a class="sourceLine" id="cb7-2" title="2"><span class="ex">vim</span> /etc/hosts</a>
<a class="sourceLine" id="cb7-3" title="3"></a>
<a class="sourceLine" id="cb7-4" title="4"><span class="ex">10.110.74.177</span> nginx.ops.com</a></code></pre></div>
<h5 id="curl测试">curl测试</h5>
<div class="sourceCode" id="cb8"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb8-1" title="1"><span class="ex">curl</span> nginx.ops.com/nginx_status</a></code></pre></div>
<p>多次curl测试会发现，流量会分发到不同版本的nginx上说明流量控制生效</p>
<h5 id="生产环境建议将多个ingress和deployment用多个文件进行创建部署方便调整流量权重防止影响系统的可用性和连续性">生产环境建议将多个ingress和deployment，用多个文件进行创建部署，方便调整流量权重，防止影响系统的可用性和连续性。</h5>
<h3 id="二基于configmap配置隔离配置灰度">二、基于configmap配置隔离（配置灰度）</h3>
<h5 id="原理为一个svc绑定多个deployment多个deployment挂载不同的configmap来实现配置隔离新配置渐进测试完后将绑定旧configmap的deployment删除即可">原理为一个svc绑定多个deployment，多个deployment挂载不同的configmap，来实现配置隔离，新配置渐进测试完后，将绑定旧configmap的deployment删除即可。</h5>
<h4 id="创建nginx-deployment和svc">1、创建nginx deployment和svc</h4>
<div class="sourceCode" id="cb9"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb9-1" title="1"><span class="ex">vim</span> nginx-deployment.yaml</a>
<a class="sourceLine" id="cb9-2" title="2"></a>
<a class="sourceLine" id="cb9-3" title="3"><span class="ex">apiVersion</span>: apps/v1</a>
<a class="sourceLine" id="cb9-4" title="4"><span class="ex">kind</span>: Deployment</a>
<a class="sourceLine" id="cb9-5" title="5"><span class="ex">metadata</span>:</a>
<a class="sourceLine" id="cb9-6" title="6">  <span class="ex">name</span>: nginx-index-v1</a>
<a class="sourceLine" id="cb9-7" title="7"><span class="ex">spec</span>:</a>
<a class="sourceLine" id="cb9-8" title="8">  <span class="ex">replicas</span>: 2  <span class="co">#初始副本数</span></a>
<a class="sourceLine" id="cb9-9" title="9">  <span class="ex">selector</span>:</a>
<a class="sourceLine" id="cb9-10" title="10">    <span class="ex">matchLabels</span>:</a>
<a class="sourceLine" id="cb9-11" title="11">      <span class="ex">app</span>: nginx</a>
<a class="sourceLine" id="cb9-12" title="12">      <span class="ex">env</span>: configmap</a>
<a class="sourceLine" id="cb9-13" title="13">  <span class="ex">strategy</span>:</a>
<a class="sourceLine" id="cb9-14" title="14">    <span class="ex">rollingUpdate</span>:</a>
<a class="sourceLine" id="cb9-15" title="15">      <span class="ex">maxSurge</span>: 1   <span class="co"># 最多比期望副本数多1个 （用于控制新版本创建速度）</span></a>
<a class="sourceLine" id="cb9-16" title="16">      <span class="ex">maxUnavailable</span>: 0  <span class="co">#更新过程中不可用的Pod数为0 （用于保证服务的连续性）</span></a>
<a class="sourceLine" id="cb9-17" title="17">    <span class="ex">type</span>: RollingUpdate</a>
<a class="sourceLine" id="cb9-18" title="18">  <span class="ex">template</span>:</a>
<a class="sourceLine" id="cb9-19" title="19">    <span class="ex">metadata</span>:</a>
<a class="sourceLine" id="cb9-20" title="20">      <span class="ex">labels</span>:</a>
<a class="sourceLine" id="cb9-21" title="21">        <span class="ex">app</span>: nginx</a>
<a class="sourceLine" id="cb9-22" title="22">        <span class="ex">env</span>: configmap</a>
<a class="sourceLine" id="cb9-23" title="23">    <span class="ex">spec</span>:</a>
<a class="sourceLine" id="cb9-24" title="24">      <span class="ex">containers</span>:</a>
<a class="sourceLine" id="cb9-25" title="25">      <span class="ex">-</span> name: nginx</a>
<a class="sourceLine" id="cb9-26" title="26">        <span class="ex">image</span>: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:1.28.0</a>
<a class="sourceLine" id="cb9-27" title="27">        <span class="ex">ports</span>:</a>
<a class="sourceLine" id="cb9-28" title="28">        <span class="ex">-</span> containerPort: 80</a>
<a class="sourceLine" id="cb9-29" title="29">        <span class="ex">volumeMounts</span>:</a>
<a class="sourceLine" id="cb9-30" title="30">        <span class="ex">-</span> name: v1</a>
<a class="sourceLine" id="cb9-31" title="31">          <span class="ex">mountPath</span>: /usr/share/nginx/html/</a>
<a class="sourceLine" id="cb9-32" title="32">      <span class="ex">volumes</span>:</a>
<a class="sourceLine" id="cb9-33" title="33">      <span class="ex">-</span> name: v1</a>
<a class="sourceLine" id="cb9-34" title="34">        <span class="ex">configMap</span>:</a>
<a class="sourceLine" id="cb9-35" title="35">          <span class="ex">name</span>: nginx-config-v1</a>
<a class="sourceLine" id="cb9-36" title="36"></a>
<a class="sourceLine" id="cb9-37" title="37"><span class="ex">---</span></a>
<a class="sourceLine" id="cb9-38" title="38"><span class="ex">apiVersion</span>: apps/v1</a>
<a class="sourceLine" id="cb9-39" title="39"><span class="ex">kind</span>: Deployment</a>
<a class="sourceLine" id="cb9-40" title="40"><span class="ex">metadata</span>:</a>
<a class="sourceLine" id="cb9-41" title="41">  <span class="ex">name</span>: nginx-index-v2</a>
<a class="sourceLine" id="cb9-42" title="42"><span class="ex">spec</span>:</a>
<a class="sourceLine" id="cb9-43" title="43">  <span class="ex">replicas</span>: 1  <span class="co">#初始副本数</span></a>
<a class="sourceLine" id="cb9-44" title="44">  <span class="ex">selector</span>:</a>
<a class="sourceLine" id="cb9-45" title="45">    <span class="ex">matchLabels</span>:</a>
<a class="sourceLine" id="cb9-46" title="46">      <span class="ex">app</span>: nginx</a>
<a class="sourceLine" id="cb9-47" title="47">      <span class="ex">env</span>: configmap</a>
<a class="sourceLine" id="cb9-48" title="48">  <span class="ex">strategy</span>:</a>
<a class="sourceLine" id="cb9-49" title="49">    <span class="ex">rollingUpdate</span>:</a>
<a class="sourceLine" id="cb9-50" title="50">      <span class="ex">maxSurge</span>: 1   <span class="co"># 最多比期望副本数多1个 （用于控制新版本创建速度）</span></a>
<a class="sourceLine" id="cb9-51" title="51">      <span class="ex">maxUnavailable</span>: 0  <span class="co">#更新过程中不可用的Pod数为0 （用于保证服务的连续性）</span></a>
<a class="sourceLine" id="cb9-52" title="52">    <span class="ex">type</span>: RollingUpdate</a>
<a class="sourceLine" id="cb9-53" title="53">  <span class="ex">template</span>:</a>
<a class="sourceLine" id="cb9-54" title="54">    <span class="ex">metadata</span>:</a>
<a class="sourceLine" id="cb9-55" title="55">      <span class="ex">labels</span>:</a>
<a class="sourceLine" id="cb9-56" title="56">        <span class="ex">app</span>: nginx</a>
<a class="sourceLine" id="cb9-57" title="57">        <span class="ex">env</span>: configmap</a>
<a class="sourceLine" id="cb9-58" title="58">    <span class="ex">spec</span>:</a>
<a class="sourceLine" id="cb9-59" title="59">      <span class="ex">containers</span>:</a>
<a class="sourceLine" id="cb9-60" title="60">      <span class="ex">-</span> name: nginx</a>
<a class="sourceLine" id="cb9-61" title="61">        <span class="ex">image</span>: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:1.28.0</a>
<a class="sourceLine" id="cb9-62" title="62">        <span class="ex">volumeMounts</span>:</a>
<a class="sourceLine" id="cb9-63" title="63">        <span class="ex">-</span> name: v2  </a>
<a class="sourceLine" id="cb9-64" title="64">          <span class="ex">mountPath</span>: /usr/share/nginx/html/</a>
<a class="sourceLine" id="cb9-65" title="65">        <span class="ex">ports</span>:</a>
<a class="sourceLine" id="cb9-66" title="66">        <span class="ex">-</span> containerPort: 80</a>
<a class="sourceLine" id="cb9-67" title="67">      <span class="ex">volumes</span>:</a>
<a class="sourceLine" id="cb9-68" title="68">      <span class="ex">-</span> name: v2</a>
<a class="sourceLine" id="cb9-69" title="69">        <span class="ex">configMap</span>:</a>
<a class="sourceLine" id="cb9-70" title="70">          <span class="ex">name</span>: nginx-config-v2</a>
<a class="sourceLine" id="cb9-71" title="71"></a>
<a class="sourceLine" id="cb9-72" title="72"></a>
<a class="sourceLine" id="cb9-73" title="73"><span class="ex">---</span></a>
<a class="sourceLine" id="cb9-74" title="74"><span class="ex">apiVersion</span>: v1</a>
<a class="sourceLine" id="cb9-75" title="75"><span class="ex">kind</span>: Service</a>
<a class="sourceLine" id="cb9-76" title="76"><span class="ex">metadata</span>:</a>
<a class="sourceLine" id="cb9-77" title="77">  <span class="ex">name</span>: nginx-svc</a>
<a class="sourceLine" id="cb9-78" title="78"><span class="ex">spec</span>:</a>
<a class="sourceLine" id="cb9-79" title="79">  <span class="ex">selector</span>:</a>
<a class="sourceLine" id="cb9-80" title="80">    <span class="ex">app</span>: nginx</a>
<a class="sourceLine" id="cb9-81" title="81">    <span class="ex">env</span>: configmap</a>
<a class="sourceLine" id="cb9-82" title="82">  <span class="ex">ports</span>:</a>
<a class="sourceLine" id="cb9-83" title="83">  <span class="ex">-</span> port: 8080</a>
<a class="sourceLine" id="cb9-84" title="84">    <span class="ex">targetPort</span>: 80</a></code></pre></div>
<h4 id="创建configmap">2、创建configmap</h4>
<div class="sourceCode" id="cb10"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb10-1" title="1"></a>
<a class="sourceLine" id="cb10-2" title="2"><span class="ex">vim</span> nginx-config.yaml</a>
<a class="sourceLine" id="cb10-3" title="3"></a>
<a class="sourceLine" id="cb10-4" title="4"><span class="ex">apiVersion</span>: v1</a>
<a class="sourceLine" id="cb10-5" title="5"><span class="ex">kind</span>: ConfigMap</a>
<a class="sourceLine" id="cb10-6" title="6"><span class="ex">metadata</span>:</a>
<a class="sourceLine" id="cb10-7" title="7">  <span class="ex">name</span>: nginx-config-v1</a>
<a class="sourceLine" id="cb10-8" title="8"><span class="ex">data</span>:</a>
<a class="sourceLine" id="cb10-9" title="9">  <span class="ex">version.html</span>: <span class="kw">|</span></a>
<a class="sourceLine" id="cb10-10" title="10">    <span class="ex">this</span> is v1 version!!</a>
<a class="sourceLine" id="cb10-11" title="11"></a>
<a class="sourceLine" id="cb10-12" title="12"><span class="ex">---</span></a>
<a class="sourceLine" id="cb10-13" title="13"><span class="ex">apiVersion</span>: v1</a>
<a class="sourceLine" id="cb10-14" title="14"><span class="ex">kind</span>: ConfigMap</a>
<a class="sourceLine" id="cb10-15" title="15"><span class="ex">metadata</span>:</a>
<a class="sourceLine" id="cb10-16" title="16">  <span class="ex">name</span>: nginx-config-v2</a>
<a class="sourceLine" id="cb10-17" title="17"><span class="ex">data</span>:</a>
<a class="sourceLine" id="cb10-18" title="18">  <span class="ex">version.html</span>: <span class="kw">|</span></a>
<a class="sourceLine" id="cb10-19" title="19">    <span class="ex">this</span> is v2 version!!</a></code></pre></div>
<h4 id="部署测试">3、部署测试</h4>
<div class="sourceCode" id="cb11"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb11-1" title="1"><span class="co">#部署deployment、svc、configmap</span></a>
<a class="sourceLine" id="cb11-2" title="2"><span class="ex">kubectl</span> apply -f nginx-config.yaml</a>
<a class="sourceLine" id="cb11-3" title="3"></a>
<a class="sourceLine" id="cb11-4" title="4"><span class="ex">kubectl</span> apply -f nginx-deployment.yaml</a>
<a class="sourceLine" id="cb11-5" title="5"></a>
<a class="sourceLine" id="cb11-6" title="6"><span class="co">#查看svc的ip</span></a>
<a class="sourceLine" id="cb11-7" title="7"><span class="ex">kubectl</span> get -f nginx-deployment.yaml</a>
<a class="sourceLine" id="cb11-8" title="8"></a>
<a class="sourceLine" id="cb11-9" title="9"><span class="ex">NAME</span>                             READY   UP-TO-DATE   AVAILABLE   AGE</a>
<a class="sourceLine" id="cb11-10" title="10"><span class="ex">deployment.apps/nginx-index-v1</span>   2/2     2            2           24h</a>
<a class="sourceLine" id="cb11-11" title="11"><span class="ex">deployment.apps/nginx-index-v2</span>   1/1     1            1           24h</a>
<a class="sourceLine" id="cb11-12" title="12"></a>
<a class="sourceLine" id="cb11-13" title="13"><span class="ex">NAME</span>                TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    <span class="ex">AGE</span></a>
<a class="sourceLine" id="cb11-14" title="14"><span class="ex">service/nginx-svc</span>   ClusterIP   10.103.74.93   <span class="op">&lt;</span>none<span class="op">&gt;</span>        8080/TCP   24h</a>
<a class="sourceLine" id="cb11-15" title="15"></a>
<a class="sourceLine" id="cb11-16" title="16"><span class="co">#测试访问</span></a>
<a class="sourceLine" id="cb11-17" title="17"><span class="ex">curl</span> 10.103.74.93:8080/version.html</a>
<a class="sourceLine" id="cb11-18" title="18"><span class="ex">this</span> is v1 version!!</a>
<a class="sourceLine" id="cb11-19" title="19"></a>
<a class="sourceLine" id="cb11-20" title="20"><span class="ex">curl</span> 10.103.74.93:8080/version.html</a>
<a class="sourceLine" id="cb11-21" title="21"><span class="ex">this</span> is v2 version!!</a>
<a class="sourceLine" id="cb11-22" title="22"></a>
<a class="sourceLine" id="cb11-23" title="23"><span class="ex">curl</span> 10.103.74.93:8080/version.html</a>
<a class="sourceLine" id="cb11-24" title="24"><span class="ex">this</span> is v1 version!!</a></code></pre></div>
</body>
</html>
